﻿Imports d = System.Drawing
Imports System.Drawing

Imports System.IO
Imports System.Drawing.Imaging

Public Class Starter
    Dim FName As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "explorer.exe")
    Dim ActiveStarterToolTip As StarterToolTip
    Dim ParentPanel1 As Panel
    Public Property ParentPanel As Panel
        Get
            Return ParentPanel1
        End Get
        Set(ByVal value As Panel)
            ParentPanel1 = value
        End Set
    End Property

    Public Property FileName()
        Get
            Return FName
        End Get
        Set(ByVal value)
            FName = value
            UpdateIcon()
        End Set
    End Property

    Private Sub Starter_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        UpdateIcon()
    End Sub

    Private Sub Icon_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Icon.MouseUp
        
    End Sub

    Private Sub UpdateIcon()
        Try
            Dim ico As d.Icon = d.Icon.ExtractAssociatedIcon(FileName)
            Dim imgs As ImageSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(ico.Handle, System.Windows.Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions)
            Icon.Source = imgs
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Icon_MouseEnter(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Icon.MouseEnter
        Dim st As New StarterToolTip
        Dim FileName As String = Path.GetFileName(FName)
        st.TextBlockMain.Text = FileName
        st.Width = FileName.Length * 6 + 40
        st.Height = 20
        ParentPanel1.ParentWindow.MyCanvas.Children.Add(st)
        Select Case DockPanel.GetDock(ParentPanel1)
            Case Dock.Top
                Canvas.SetLeft(st, Canvas.GetLeft(Me) - (Me.Height) - (st.Height / 2))
                Canvas.SetTop(st, ParentPanel1.Height)
            Case Dock.Left
                Canvas.SetLeft(st, ParentPanel1.Width)
                Canvas.SetTop(st, Canvas.GetTop(Me) + (Me.Height / 2) - (st.Height / 2))
            Case Dock.Bottom
                Canvas.SetLeft(st, Canvas.GetLeft(Me) + (Me.Width / 2) - (st.Width / 2))
                Canvas.SetTop(st, ParentPanel1.ParentWindow.Height - st.Height - ParentPanel1.Height - 24)
            Case Dock.Right
                Canvas.SetLeft(st, ParentPanel1.ParentWindow.Width - ParentPanel1.Width - st.Width)
                Canvas.SetTop(st, Canvas.GetTop(Me) + (Me.Height / 2) - (st.Height / 2))
        End Select
        ActiveStarterToolTip = st

        Dim b As New d.Bitmap(CInt(Me.Width), CInt(Me.Height))
        Dim g As d.Graphics = d.Graphics.FromImage(b)

        Try
            g.DrawIcon(d.Icon.ExtractAssociatedIcon(FName), New d.Rectangle(0, 0, b.Width, b.Height))
        Catch ex As Exception

        End Try
        SetBrightness(0.1, b, g)
        Dim imgs As ImageSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(b.GetHbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())

        Icon.Source = imgs
    End Sub

    Sub SetBrightness(ByVal Brightness As Single, ByVal img As Image, ByVal g As Graphics)
        Dim colorMatrixVal As Single()() = { _
           New Single() {1, 0, 0, 0, 0}, _
           New Single() {0, 1, 0, 0, 0}, _
           New Single() {0, 0, 1, 0, 0}, _
           New Single() {0, 0, 0, 1, 0}, _
           New Single() {Brightness, Brightness, Brightness, 0, 1}}
        Dim colorMatrix As New Imaging.ColorMatrix(colorMatrixVal)
        Dim ia As New ImageAttributes
        ia.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
        g.DrawImage(img, New Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia)
    End Sub

    Private Sub Icon_MouseLeave(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Icon.MouseLeave
        ParentPanel1.ParentWindow.MyCanvas.Children.Remove(ActiveStarterToolTip)
        Dim b As New d.Bitmap(CInt(Me.Width), CInt(Me.Height))
        Dim g As d.Graphics = d.Graphics.FromImage(b)

        Try
            g.DrawIcon(d.Icon.ExtractAssociatedIcon(FName), New d.Rectangle(0, 0, b.Width, b.Height))
        Catch ex As Exception

        End Try
        SetBrightness(0, b, g)
        Dim imgs As ImageSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(b.GetHbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())

        Icon.Source = imgs
    End Sub

    Private Sub EditStarter(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MenuItem1.Click
        ParentPanel1.EditStarter(Me)
    End Sub

    Private Sub Icon_MouseLeftButtonUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Icon.MouseLeftButtonUp
        Try
            Dim p As Process = Process.Start(FName)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MenuItem2.Click
        ParentPanel1.DeleteStarter(Me)
    End Sub
End Class
